package com.example.carefree_server.controller.c_departmentController;

import jakarta.servlet.http.HttpServletResponse;
import net.glxn.qrgen.core.image.ImageType;
import net.glxn.qrgen.javase.QRCode;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;

@RestController
@RequestMapping("/signInForm")
public class QRCodeController {

    @GetMapping("/generateQRCode")
    public void generateQRCode(@RequestParam("qrtext") String qrtext, HttpServletResponse response) throws Exception {
        ByteArrayOutputStream out = QRCode.from(qrtext).to(ImageType.PNG).stream();
        response.setContentType("image/png");
        response.setContentLength(out.size());
        OutputStream outStream = response.getOutputStream();
        outStream.write(out.toByteArray());
        outStream.flush();
        outStream.close();
    }
}